package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.repositoryService.InfrastructureRepositoryService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.inforConst.InfrastructureAddRequest;
import com.ktwlsoft.archivemanage.payload.inforConst.InfrastructureUpdateRequest;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/infrastructure")
public class InfrastructureController {

    @Autowired
    private InfrastructureRepositoryService infrastructureRepositoryService;

    @ApiOperation(value = "查询基础设施信息")
    @RequestMapping(value = "/findOne",produces = "application/json",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> findInfrastructureInfo(@ApiParam(name = "unitId",value = "立档单位id")
                                                    @RequestParam(defaultValue = "",required = true) Long unitId){
        Result result=infrastructureRepositoryService.findInfrastructureInfo(unitId);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "新增基础设施信息")
    @RequestMapping(value = "/addOne",produces = "application/json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> addInfrastructureInfo(@RequestBody InfrastructureAddRequest request,
                                                   BindingResult bindingResult){
        Result result=null;
        if(bindingResult.hasErrors()){
            result=Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result=infrastructureRepositoryService.addInfrastructureInfo(request);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "更新基础设施信息")
    @RequestMapping(value = "/updateOne",produces = "application/json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> updateInfrastructureInfo(@RequestBody InfrastructureUpdateRequest request,
                                                      BindingResult bindingResult){
        Result result=null;
        if(bindingResult.hasErrors()){
            result=Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result=infrastructureRepositoryService.updateInfrastructureInfo(request);
        return ResponseEntity.ok(result);
    }

}
